package com.zsc.wxm.ysh.anjo.member.controller;


import com.zsc.wxm.ysh.anjo.member.service.MemberService;
import com.zsc.wxm.ysh.anjo.result.PageResult;
import com.zsc.wxm.ysh.anjo.result.Result;
import com.zsc.wxm.ysh.anjo.service.base.model.member.dto.AdminRegisterDTO;
import com.zsc.wxm.ysh.anjo.service.base.model.member.dto.MemberInfoDTO;
import com.zsc.wxm.ysh.anjo.service.base.model.member.qo.MemberQuery;
import com.zsc.wxm.ysh.anjo.service.base.model.member.vo.RegisterMemberVO;
import com.zsc.wxm.ysh.anjo.service.base.model.member.vo.UserInfoVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author playmaker
 * @since 2021-01-04
 */
@RestController
@RequestMapping("/user/member")
@Validated
public class MemberController {

    @Resource
    private MemberService memberService;

    @PostMapping("/register")
    @ApiOperation("新用户注册")
    public Result register(@Validated @RequestBody RegisterMemberVO registerMemberVo) {
        memberService.registerV2(registerMemberVo);
        return Result.ok("注册成功");
    }

    @PostMapping("/admin-register")
    @ApiOperation("管理员注册")
    public Result adminRegister(@Validated @RequestBody AdminRegisterDTO params) {
        memberService.adminRegister(params);
        return Result.ok("注册成功");
    }

    @PostMapping("/login-by-phone")
    @ApiOperation("通过手机号验证码登录")
    public Result loginByPhone(@NotNull(message = "电话号码不能是空的") @RequestParam("phone") String phone,
                               @NotNull(message = "验证码不能是空的") @RequestParam("code") String code) {
        Map<String, Object> data = new HashMap<>();
        data.put("token", memberService.loginByPhone(phone, code));
        return Result.ok("登录成功", data);
    }

    @PostMapping("/update-member-info")
    @ApiOperation("更新用户信息")
    public Result updateMemberInfo(@RequestBody MemberInfoDTO params) {
        memberService.update(params);
        return Result.ok("修改成功");
    }

    @PostMapping("/list")
    @ApiOperation("根据查询条件查询用户VO,包含权限信息")
    public Result list(@RequestBody MemberQuery memberQuery) {
        Map<String, Object> data = new HashMap<>();
        List<UserInfoVO> list = memberService.getListByQo(memberQuery);
        data.put("list", list);
        return Result.ok("获取成功", data);
    }

    @GetMapping("/user-info")
    @ApiOperation("获取登录用户的信息")
    public Result userInfo(HttpServletRequest request) {
        Map<String, Object> data = new HashMap<>();
        data.put("userInfo", memberService.getUserInfo(request));
        return Result.ok("成功", data);
    }

    @PostMapping("/update-avatar")
    @ApiOperation("更新用户头像")
    public Result updateAvatar(@RequestParam("id") Long id, @RequestParam("file") MultipartFile file) {
        memberService.updateAvatar(id, file);
        return Result.ok("更换头像成功");
    }


    @PostMapping("/get-one")
    @ApiOperation("根据id获取用户信息")
    public Result getOne(@RequestBody MemberQuery memberQuery) {
        Map<String, Object> data = new HashMap<>();
        data.put("memberBo", memberService.getOneByQo(memberQuery));
        return Result.ok("获取成功", data);
    }

    @PostMapping("/get-member-list")
    @ApiOperation("根据查询条件查询用户列表，不包含权限信息")
    public Result getMemberList(@RequestBody MemberQuery memberQuery) {
        Map<String, Object> data = new HashMap<>();
        data.put("list", memberService.getList(memberQuery));
        return Result.ok("获取成功", data);
    }

    @PostMapping("/get-landlord-infos")
    @ApiOperation("获取房东房源信息")
    public PageResult getLandlordInfos(@RequestBody MemberQuery query) {
        return memberService.getLandlordInfos(query);
    }

    @PostMapping("/get-list-by-ids")
    @ApiOperation("批量获取用户数据")
    public Result getListByIds(@RequestBody List<Long> ids) {
        Map<String, Object> data = new HashMap<>();
        data.put("list", memberService.getListByIds(ids));
        return Result.ok("获取成功", data);
    }
}

